#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileInfo>
#include <QDebug>
#include <QScreen>
#include <QDropEvent>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QMessageBox>
#include <QIcon>
#include <src/utils/deepinappearance.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_iconLab(nullptr)
{
    ui->setupUi(this);

    setAcceptDrops(true);

    setWindowTitle("Deepin 壁纸直接设置工具");
    m_iconLab = new QLabel(this);
    m_iconLab->setAlignment(Qt::AlignCenter);
    QIcon icon("://assets/icon.png");
    QList localAvailableSizes = icon.availableSizes();
    QPixmap localPixmap = icon.pixmap(300, 300);
    localPixmap.setDevicePixelRatio(qApp->primaryScreen()->devicePixelRatio());
    m_iconLab->setPixmap(localPixmap);
    m_iconLab->setFixedSize(localPixmap.width(),localPixmap.height());
    int localWidth = m_iconLab->size().width();
    m_iconLab->move((this->rect().center() - (QRect(0,0, m_iconLab->size().width(), m_iconLab->size().height()).center())));
    m_iconLab->lower();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    const QMimeData *localMimeData = event->mimeData();
    if (localMimeData->hasUrls()) {
        if (localMimeData->urls().at(0).isLocalFile()) {
            return event->accept();
        }
    }
}

void MainWindow::dropEvent(QDropEvent *event)
{
    QString localFile = event->mimeData()->urls()[0].toLocalFile();;
    QPixmap pixmap(localFile);
    if (pixmap.isNull()) {
        return;
    }

    auto btn = QMessageBox::information(this, "设置背景", "确认设置为桌面壁纸吗", QMessageBox::Ok | QMessageBox::Cancel);
    if (btn == QMessageBox::Ok) {
        DeepinAppearance::setBackGround(localFile);
    }
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    m_iconLab->move((this->rect().center() - (QRect(0,0, m_iconLab->size().width(), m_iconLab->size().height()).center())));
}
